/*
 * This is where the program runs.  Initially it generates 500 points randomly on the canvas.
 * The number of generated points could be changed in the "generatePoints(500);" sentence.
 */

package Test;

import javax.swing.JFrame;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import MyTree.MyNode;
import MyTree.MyTree;

public class GUI extends JFrame{

	private MyTree tree;
	private MyCanvas canvas;
	private ArrayList<Point> allPoints;
	private ArrayList<Point> highlight;
	
	public static void main(String args[]){
		new GUI();
	}
	
	public GUI(){
		this.setTitle("Range Tree Search");
		this.setLocation(100, 100);
		this.setSize(800, 600);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		generatePoints(1000);
		
		tree = new MyTree();
		for(Point p : allPoints){
			tree.insert(new MyNode(p.x, p.y));
		}
		tree.buildAssoTree();
		
		canvas = new MyCanvas(this);
		canvas.setAllPoints(allPoints);
		
		this.add(canvas);
		
		this.setVisible(true);
	}
	
	public void query(Point p1, Point p2){
		
//		for(int i = 0; i < 10000; i++)
		highlight = tree.query(Math.min(p1.x, p2.x), Math.max(p1.x, p2.x), 
							   Math.min(p1.y, p2.y), Math.max(p1.y, p2.y));
		
//		for(int i = 0; i < 10000; i++)
//			highlight = tree.query2(Math.min(p1.x, p2.x), Math.max(p1.x, p2.x), 
//								   Math.min(p1.y, p2.y), Math.max(p1.y, p2.y));
		canvas.setHighLight(highlight);
	}
	
	private void generatePoints(int num){
		if(num > 0){
			Random generator = new Random();
			allPoints = new ArrayList<Point>();
			
			for(int i = 0; i < num; i++){
				allPoints.add(new Point(generator.nextInt(800), generator.nextInt(600)));
			}
		}
	}
}
